//
//  MBProgressHUD+YCExtension.m
//  YCPublicDemo
//
//  Created by dongshangtong on 2017/5/22.
//  Copyright © 2017年 xinhuanwangluo. All rights reserved.
//

#import "MBProgressHUD+YCExtension.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"
#import <UIKit/UIKit.h>

@implementation MBProgressHUD (YCExtension)



/**** Loading ****/

+ (instancetype)showLoading
{
    return [self showLoadingToView:nil];
}

+ (instancetype)showLoadingToView:(UIView *)view
{
    [self dismissForView:view];
    
    view = view ? view : [UIApplication sharedApplication].keyWindow ;
    
    MBProgressHUD *hud = [self showHUDAddedTo:view animated:YES];
    hud.backgroundColor = [UIColor clearColor];
    hud.userInteractionEnabled = NO;
    [hud hide:YES afterDelay: MAXFLOAT];
        
    return hud;
}

+ (instancetype)showLoadingWithStatus:(NSString *)status
{
    return [self showLoadingWithStatus:status toView:nil];
}

+ (instancetype)showLoadingWithStatus:(NSString *)status toView:(UIView *)view
{
    MBProgressHUD *hud = [self showLoadingToView:view];
    hud.labelText = status;
    
    return hud;
}



/**** Success ****/

+ (void)showSuccess
{
    [self showSuccessWithStatus:@"成功"];
}

+ (void)showSuccessWithStatus:(NSString *)status
{
    [self showSuccessWithStatus:status toView:nil];
}

+ (void)showSuccessWithStatus:(NSString *)status toView:(UIView *)view
{
    [self show:status icon:@"success" view:view];
}

/**** Error ****/

+ (void)showError
{
    [self showErrorWithStatus:@"失败"];
}

+ (void)showErrorWithStatus:(NSString *)status
{
    [self showErrorWithStatus:status toView:nil];
}

+ (void)showErrorWithStatus:(NSString *)status toView:(UIView *)view
{
     [self show:status icon:@"error" view:view];
}

/**** Info ****/

+ (void)showInfo
{
    [self showInfoWithStatus:@"未完操作"];
}

+ (void)showInfoWithStatus:(NSString *)status
{
    return [self showInfoWithStatus:status toView:nil];
}

+ (void)showInfoWithStatus:(NSString *)status toView:(UIView *)view
{
    [self show:status icon:@"info" view:view];

}




#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    //hud.dimBackground =YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:3];
}



/**** 有动画加载 ****/

+(void)showLoadingAnimationHUD{
    
    
    [self showLoadingAnimationDataHUDForicon:nil];
    
}

+(void)showLoadingAnimationDataHUDForicon:(NSString *)icon{
    
    
    [self showLoadingAnimationDataHUDForView:nil icon:icon];
    
}


+(void)showLoadingAnimationDataHUDForView:(UIView *)view  icon:(NSString *)icon
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    NSString * imgaName;
    if (icon) {
      imgaName  = icon;
    }else{
    imgaName = @"MBProgressHUD.bundle/loadinggif3.gif";
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:imgaName ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path];

    UIImageView *imgView =[[UIImageView alloc]init];
    imgView.image = [UIImage sd_animatedGIFWithData:data];
    imgView.frame =CGRectMake(0, 0, 100, 100);
    imgView.contentMode = UIViewContentModeScaleToFill;
    //imgView.backgroundColor = [UIColor whiteColor];
    hud.customView= imgView;
    hud.minSize= CGSizeMake(100, 100);
   // hud.dimBackground = YES; //有一点蒙版感觉
    hud.removeFromSuperViewOnHide = YES;
    //hud.backgroundColor = [UIColor whiteColor];
    hud.color = [UIColor whiteColor];
}



/**** Dismiss --移除****/

+ (void)dismiss
{
    [self dismissForKeyWindow];
    
    [self dismissForView:nil];
}

+ (void)dismissForKeyWindow
{
    UIView *keyWindow = [UIApplication sharedApplication].keyWindow ;
    [self hideHUDForView:keyWindow animated:YES];
}

+ (void)dismissForView:(UIView *)view
{
    if (view) [self hideHUDForView:view animated:YES];
}

@end
